﻿function Widget_FlickrBrowser(obj)
{
   this.initializeBase(obj);
}
Widget_FlickrBrowser.prototype = new WidgetInstance;

Widget_FlickrBrowser.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
      this.Refresh();
   }
}

Widget_FlickrBrowser.prototype.Edit = function()
{
   this.Widget.InstanceEdit();
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      if(this.inEditMode)
         this.Settings_TxtCountryCode.focus();
   }   
}

Widget_FlickrBrowser.prototype.Refresh = function()
{
   if (this.Settings_txtSearch.value.length > 0)
   {
      this.FeedURL = "http://api.flickr.com/services/feeds/photos_public.gne?tags=" + this.Settings_txtSearch.value + "&lang=en-us&format=rss2";
      Proxy.GetRssFeed(this.FeedURL,0,10,false,Function.createDelegate( this, this.onContentLoad ));
   }
   else
   {
      this.WidgetDiv_BodyContent.innerHTML = "No search term defined";
   }
}

Widget_FlickrBrowser.prototype.onContentLoad = function(rss)
{
   WriteHistory("Widget_FlickrBrowser.prototype.onContentLoad #" + this.id);
     var div = this.WidgetDiv_BodyContent;
     div.innerHTML = "";
     
     if (!rss)
     {
        div.innerHTML = "Search returned no items";
        return;
     }
     else if (rss.length < 1)
     {
      div.innerHTML = "Search returned no items";
     }

     // creates a <table> element and a <tbody> element
     
     for(var i=0;i<rss.length;i++)
     {
         var item = rss[i];
            
         //First display as text
         var span = document.createElement("span");
         
         var content = item.Title + "<br>" + (item.ContentEncoded.length < 1 ? "<span class='feed_item_link'>" + item.Description + "</span>": item.ContentEncoded);

         span.innerHTML = content;
         
         div.appendChild(span);
      }      
}

Widget_FlickrBrowser.prototype.Load = function()
{
   this.Menu_Edit = new Array();
   this.Menu_Edit.push("m" + this.id + "_edit");
   this.Widget.InstanceLoad();
      
   this.WidgetDiv_BodySettings = $get('wdivbs' + this.id);
   this.WidgetDiv_BodyContent = document.getElementById("wdivbc" + this.Widget.id);
   this.WidgetDiv_Command_Edit = document.getElementById("wdivc_edit" + this.Widget.id);
   
   this.Settings_txtSearch = $get('txtSearch' + this.id);
   this.Settings_txtSearch.value = this.Widget.obj.State;
   this.Refresh();  
   
  this.Widget.InstanceLoadComplete();
}

Widget_FlickrBrowser.prototype.Save = function()
{
   this.Widget.obj.State = this.Settings_txtSearch.value;
   
   //Save only if have permission, else ignore
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
      
   this.Refresh();   
}

Widget_FlickrBrowser.prototype.SearchKeyPress = function(e)
{
   var keynum;

   if(window.event) // IE
     keynum = e.keyCode;
   else if(e.which) // Netscape/Firefox/Opera
     keynum = e.which;
     
  if (keynum == 13)
  {
     this.Save();
     return false;
  }
}